<?php

namespace app\acc\controller;


use app\common\logic\CartLogic;
use app\common\logic\UsersLogic;
use app\common\model\Users;
use think\Controller;
use think\Session;
use think\Request;
use think\View;
use think\Config;
use think\Response;
use think\exception\DbException;
use think\exception\HttpResponseException;
use think\Db;

class Sign extends Base {
    Public function _initialize(){

        parent::_initialize();

        // 没有登录
        $account = $this->getAccount();
        if($this->request->get('_uid')){
            $id = (int) $this->request->get('_uid');
            $member = M("member")->where("(account='".$id."' or id='".$id."') AND isDelete=2")->find();
            parent::setAccount($member);
        }
        if(empty($account)){
            if (IS_AJAX){
                exit( $this->ajaxLoginError("请登录"));
            }else{
                $this->redirect(U("Login/login"));
            }
        }

        $this->detection_order();
        $this->currencyrise();
    }
    //退出登录
    public function exitLogin()
    {
        session_unset();
        // session_destroy();
        parent::remove('account');
        parent::remove('token');
        parent::remove('code');
        parent::remove("openid");
        setcookie('account', '', time() - 3600, '/');
        header("Location:" . U('acc/Index/index'));
        exit;
        // $this->ajaxSuccess("退出成功");
    }

    public function currencyrise(){
        //获取最近一条信息
        $currency_info = Db::name('currencyrise_log')->order('update_time desc')->find();
        $config = worldCa('basic');

        //获取时间
        $now_time = date('Ymd',time());
        $update_time = date('Ymd',$currency_info['update_time']);

        if($now_time <= $update_time)return true;


        $days = $currency_info ?$now_time-$update_time : 1;

        //计算价值
        if($config['ACC_float_type'] == 1){//等差算法
            $add_price = floor($config['exchange_change']*$config['ACC_float']*$days)/100;//保留两位小数
            $now_price = floor(($add_price+$config['dc_cny_rate'])*100)/100;
        }else{//等比算法
            $now_price = $config['dc_cny_rate'];//记录数值
            $add_price = 0;//累计数值
            for ($x=0; $x<$days; $x++) {
                $add_price = floor(($add_price+$now_price*$config['ACC_float']/100)*100)/100;
                $now_price = floor(($now_price+$now_price*$config['ACC_float']/100)*100)/100;
            }
       
        }


        $currenct_data = array(
            'rate' => $config['ACC_float'],
            'before_price' => $config['dc_cny_rate'],
            'add_price' => $add_price,
            'after_price' => $now_price,
            'update_time' => time()
        );

        $result = Db::name('currencyrise_log')->add($currenct_data);
        if($result){
            worldCa('basic',array('dc_cny_rate'=>$now_price));
        }
    }

    // 获取账号信息
    function getAccount(){
        // 获取token
        $memberId = $this->get('account')['id'];
        $member = M('member')->where(array("id"=>$memberId, "isDisable"=>2, "isDelete"=>2))->find();
        if ($member){
            session('account', $member);
        } else {
            session('account', null);
        }
        return $this->get('account');
    }

    // 获取账号ID
    function getAccountId(){
        return $this->getAccount()['id'];
    }
}